class BusinessProcessClassesController < ApplicationController
  in_place_edit_for :business_process_class, :name
  
  layout 'application', :except => :new
  
  # GET /business_process_classes
  # GET /business_process_classes.xml
  def index
    @business_process_classes = BusinessProcessClass.roots

    respond_to do |format|
      format.html # index.html.erb
      format.xml  { render :xml => @business_process_classes }
    end
  end

  # GET /business_process_classes/1
  # GET /business_process_classes/1.xml
  def show
    @business_process_class = BusinessProcessClass.find(params[:id])

    respond_to do |format|
      format.html # show.html.erb
      format.xml  { render :xml => @business_process_class }
    end
  end

  # GET /business_process_classes/new
  # GET /business_process_classes/new.xml
  def new
    @business_process_class = BusinessProcessClass.new

    respond_to do |format|
      format.html # new.html.erb
      format.xml  { render :xml => @business_process_class }
    end
  end

  # GET /business_process_classes/1/edit
  def edit
    @business_process_class = BusinessProcessClass.find(params[:id])
  end

  # POST /business_process_classes
  # POST /business_process_classes.xml
  def create
    @business_process_class = BusinessProcessClass.new(params[:business_process_class])

    respond_to do |format|
      if @business_process_class.save
        flash[:notice] = 'Новый класс бизнес-процессов был создан.'
        format.html { redirect_to(@business_process_class) }
        format.xml  { render :xml => @business_process_class, :status => :created, :location => @business_process_class }
        format.js
      else
        format.html { render :action => "new" }
        format.xml  { render :xml => @business_process_class.errors, :status => :unprocessable_entity }
      end
    end
  end

  # PUT /business_process_classes/1
  # PUT /business_process_classes/1.xml
  def update
    @business_process_class = BusinessProcessClass.find(params[:id])

    respond_to do |format|
      if @business_process_class.update_attributes(params[:business_process_class])
        flash[:notice] = 'Класс бизнес-процессов был изменен.'
        format.html { redirect_to(@business_process_class) }
        format.xml  { head :ok }
      else
        format.html { render :action => "edit" }
        format.xml  { render :xml => @business_process_class.errors, :status => :unprocessable_entity }
      end
    end
  end

  # DELETE /business_process_classes/1
  # DELETE /business_process_classes/1.xml
  def destroy
    @business_process_class = BusinessProcessClass.find(params[:id])
    @business_process_class.destroy

    respond_to do |format|
      format.html { redirect_to(business_process_classes_url) }
      format.xml  { head :ok }
      format.js
    end
  end
  
  def sort
    @branch_classes = BusinessProcessClass.find(params[:id])
    @branch_classes.children.each do |branch_class|
      branch_class.position = params['branch_' + params[:id]].index(branch_class.id.to_s) + 1
      branch_class.save
    end
    render :nothing => true
  end
end
